﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="ProcessFactory.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    03/02/2013 21:59 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using System;
using System.Net;

#endregion

namespace IveelySE.Spider
{
    /// <summary>
    ///   信息内容处理工厂（工厂模式）
    /// </summary>
    public static class ProcessFactory
    {
        /// <summary>
        ///   信息内容处理
        /// </summary>
        /// <param name="requestUrl"> 请求的URL </param>
        /// <param name="referrerUrl"> 引用URL </param>
        /// <param name="webResponse"> web请求的响应结果 </param>
        /// <returns> </returns>
        public static BasicContent Create(Uri requestUrl, Uri referrerUrl, HttpWebResponse webResponse)
        {
            //获取媒体类型
            var mimeType = webResponse.GetMimeType();
            //如果是HTML
            if(BasicContent.IsHtml(mimeType))
            {
                //交给HTML处理器
                return new HtmlContent(requestUrl, referrerUrl, webResponse);
            }
            //else if (HttpCrawlerContent.IsXml(mimeType))
            //{
            //    return new XmlHttpCrawlerContent(requestUrl, referrerUrl, webResponse);
            //}

            return new BasicContent(requestUrl, referrerUrl, webResponse);
        }

        public static string GetMimeType(this WebResponse response)
        {
            var contentType = response.Headers[HttpResponseHeader.ContentType];

            var mimeType = contentType.Split(';')[0];

            return mimeType;
        }
    }
}